//
//  StoreDetailsFooterView.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/4/28.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit

class StoreDetailsFooterView: UIView {

    var imageArray = [PhotoModel]()
    
    @IBOutlet var storeNameTF: UITextField!
    
    @IBOutlet var storeTypeBtn: UIButton!
    
    
    @IBOutlet var chooseStoreAddressBtn: UIButton!
    
    @IBOutlet var storeDetailsAddressTF: UITextField!
    
    
    @IBOutlet var chooseLocationBtn: UIButton!
    @IBOutlet var phoneTF: UITextField!
    
    @IBOutlet var isInstallPointBtn: UIButton!
    
    @IBOutlet var addImageCollectionview: UICollectionView!
    
    @IBOutlet var collectionviewHeight: NSLayoutConstraint!
    
    
    @IBOutlet var TagRunnerImageView: UIImageView!
    
    @IBOutlet var tagRunnerBtn: UIButton!
    
    @IBOutlet var introductionTF: UITextField!
    
    
    @IBOutlet var detailsTF: UITextField!
    
    typealias ImageDidChooseBlock = (_ intValue:Int) -> Void
    
    var tapImageDid:ImageDidChooseBlock?
    
    func callBackBlock(_ block: @escaping ImageDidChooseBlock) {
        
        tapImageDid = block
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        configView()
    }
    
    class func getStoreDetailsFooterView() -> StoreDetailsFooterView {
        return UIView.creatFromNIB(By: "StoreDetailsFooterView") as! StoreDetailsFooterView
    }
    
    
    
    func configView() -> Void {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 1
        addImageCollectionview.frame = .zero
        addImageCollectionview.collectionViewLayout = layout
        addImageCollectionview.delegate = self
        addImageCollectionview.dataSource = self
         addImageCollectionview.register(UINib(nibName: "AddImageCollectionViewCell", bundle: nil),forCellWithReuseIdentifier: "imageCell")
        addImageCollectionview.showsVerticalScrollIndicator = false
        
    }
    
    
    
    
    
}

    //MARK:delegate
extension StoreDetailsFooterView: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
    {
        
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }
        
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            if (imageArray.count) > 0 {
                if imageArray.count == 5 {
                    return imageArray.count
                }
                return imageArray.count + 1
            }
            return 1
        }
        
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as!AddImageCollectionViewCell
            cell.deleteBtn.isHidden = false
            if imageArray.count > 0 {
                if indexPath.item == imageArray.count {
                cell.cellImageView.image = UIImage.init(named: "addImage")
                cell.deleteBtn.isHidden = true                }else {
                let photoModel = imageArray[indexPath.item] ;
                cell.cellImageView.sd_setImage(with: NSURL(string: photoModel.url )! as URL, placeholderImage: UIImage.init(named: "addImage"))
                }
            }else {
                cell.cellImageView.image = UIImage.init(named: "addImage")
                cell.deleteBtn.isHidden = true
            }
            cell.callBackBlock { (index) in
                self.imageArray.remove(at: indexPath.item)
                self.addImageCollectionview.reloadData()
            }
            return cell
        }
        
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if tapImageDid != nil {
                tapImageDid!(indexPath.item)
            }
        }
        
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            
            let w: CGFloat = ScaleWidth(100.0)
            let h: CGFloat = ScaleHeight(100.0)
            return CGSize(width: w, height: h)
        }
        
        //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        //        return CGSize(width: kScreenWidth - 20, height: 10)
        //    }
    
    
}

